package com.shark.study.mapper;


import com.shark.study.entities.TestPO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * @auther shark
 * @create 2024-07-07 16:46
 */
public interface TestMapper {

    void _batchInsert(@Param("tableName") String tableName, @Param("list") List<TestPO> list);

    default void batchInsert(String tableName, List<TestPO> list) {
        int batchSize = 1000;
        int size = list.size();
        int batch = size % batchSize == 0 ? size / batchSize : size / batchSize + 1;
        for (int i = 0; i < batch; i++) {
            int fromIndex = i * batchSize;
            int toIndex = fromIndex + batchSize;
            if (toIndex > size) {
                toIndex = size;
            }
            List<TestPO> insertList = list.subList(fromIndex, toIndex);
            this._batchInsert(tableName, insertList);
        }
    }


    @Delete("delete from ${tableName}")
    void delete(@Param("tableName") String tableName);
}